<template>
  <d2-container class="page">
    <el-dialog
      width="600px"
      :title="createOrEdit"
      :visible.sync="dialogFormVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-form :model="form" ref="form">
        <el-form-item label="手机号" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.mobile" ></el-input>
        </el-form-item>
        <el-form-item label="积分" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.integral" ></el-input>
        </el-form-item>
        <el-form-item label="是否开通" :label-width="formLabelWidth">
          <el-switch v-model="form.memberFlag" active-color="#13ce66" inactive-color="#ff4949" @change="onChange"></el-switch>
        </el-form-item>
        <el-form-item label="会员等级" :label-width="formLabelWidth">
          <el-input v-model="form.memberLevel" @change="onChange"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button v-if="createOrEdit=='新建'" type="primary" @click="handleCreateNew('form')">确 定</el-button>
        <el-button v-else type="primary" @click="handleCreateNew('form')">修 改</el-button>
      </div>
    </el-dialog>
    <el-button type="primary" size="mini" @click="getList()">刷新</el-button>
    <el-divider></el-divider>
    <el-table
      stripe
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column type="index" label="序号"></el-table-column>
      <!-- <el-table-column label="id" width="120">
        <template slot-scope="scope">{{ scope.row.id }}</template>
      </el-table-column>-->
      <el-table-column label="昵称">
        <template slot-scope="scope">{{ scope.row.nickname }}</template>
      </el-table-column>
      <el-table-column label="头像" width="120">
        <template slot-scope="scope">
          <img v-bind:src="scope.row.headImgUrl" style="width:55%" />
        </template>
      </el-table-column>
      <el-table-column label="电话">
        <template slot-scope="scope">{{ scope.row.mobile }}</template>
      </el-table-column>
      <el-table-column label="姓名">
        <template slot-scope="scope">{{ scope.row.realName }}</template>
      </el-table-column>
      <el-table-column label="积分">
        <template slot-scope="scope">{{ scope.row.integral }}</template>
      </el-table-column>
      <el-table-column label="是否开通">
        <template slot-scope="scope">{{ scope.row.memberFlag }}</template>
      </el-table-column>
      <el-table-column label="会员级别">
        <template slot-scope="scope">{{ scope.row.memberLevel }}</template>
      </el-table-column>
      <el-table-column label="创建时间" width="200">
        <template
          slot-scope="scope"
        >{{ scope.row.createTime | format2DateTime('{y}-{m}-{d} {h}:{m}:{s}') }}</template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="showEditDlg(scope.row)" icon="el-icon-edit"></el-button>
          <!-- <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)"
            icon="el-icon-delete"
          ></el-button>-->
        </template>
      </el-table-column>
    </el-table>
    <br />
    <el-pagination
      background
      layout="prev, pager, next"
      :page-size="25"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></el-pagination>
  </d2-container>
</template>

<script>
import * as membership from "@/api/shop.membership";

export default {
  components: {},
  data() {
    return {
      fileList: [],
      tableData: [],
      multipleSelection: [],
      dialogFormVisible: false,
      form: {},
      formLabelWidth: "100px",
      dialogVisible: false,
      createOrEdit: "",
      listQuery: {
        pageStart: 1,
        pageSize: 25
      },
      total: 0
    };
  },
  filters: {
    format2DateTime: function(time, cFormat) {
      time = Date.parse(new Date(time));
      if (arguments.length === 0) {
        return null;
      }

      if ((time + "").length === 10) {
        time = +time * 1000;
      }

      const format = cFormat || "{y}-{m}-{d} {h}:{i}:{s}";
      let date;
      if (typeof time === "object") {
        date = time;
      } else {
        date = new Date(parseInt(time));
      }
      const formatObj = {
        y: date.getFullYear(),
        m: date.getMonth() + 1,
        d: date.getDate(),
        h: date.getHours(),
        i: date.getMinutes(),
        s: date.getSeconds(),
        a: date.getDay()
      };
      const timeStr = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
        let value = formatObj[key];
        if (key === "a")
          return ["一", "二", "三", "四", "五", "六", "日"][value - 1];
        if (result.length > 0 && value < 10) {
          value = "0" + value;
        }
        return value || 0;
      });
      return timeStr;
    }
  },
  mounted() {
    this.getList();
    this.$route.meta.title = "会员管理";
  },
  methods: {
    onChange(e) {
      console.log("onChange", e);
      this.form.integral = 0;
    },

    handleSizeChange(e) {
      console.log("handleSizeChange", e);
    },

    handleCurrentChange(e) {
      console.log("handleCurrentChange", e);
      this.listQuery.pageStart = e;
      this.getList();
    },

    showCreateDlg(formName) {
      this.dialogFormVisible = true;
      this.createOrEdit = "新建";
      // this.$refs[formName].resetFields();
    },

    showEditDlg(row) {
      this.dialogFormVisible = true;
      this.createOrEdit = "编辑";
      this.form = row;
    },

    handleUploadSuccess(response, file, fileList) {
      console.log(response);
      if (response.success) this.downloadPath = response.downloadPath;
    },

    handleCreateNew(formName) {
      let that = this;

      const set = that.$refs;
      set[formName].validate(valid => {
        if (valid) {
          if (this.createOrEdit == "新建") {
            
          }

          if (this.createOrEdit == "编辑") {
            let input = [
              { phone: that.form.mobile, integral: that.form.integral }
            ];
            membership.updateIntegral(input).then(() => {
              // that.dialogFormVisible = false;
              // that.getList();
              // that.$notify({
              //   title: "成功",
              //   message: "创建成功",
              //   type: "success",
              //   duration: 2000
              // });
            });

            input = [
              {
                id: that.form.shopPersonnelRelationId,
                memberFlag: that.form.memberFlag,
                memberLevel: that.form.memberLevel
              }
            ];
            membership.updateMemberLevel(input).then(() => {
              that.dialogFormVisible = false;
              that.getList();
              that.$notify({
                title: "成功",
                message: "操作成功",
                type: "success",
                duration: 2000
              });
            });
          }
        } else {
          return false;
        }
      });
    },

    getList() {
      membership.fetchList(this.listQuery).then(response => {
        if (response.success) {
          this.tableData = response.data.list;
          this.total = response.data.totalCount;
        }
      });
    },

    handleRemove(file, fileList) {
      console.log(file, fileList);
    },

    handleDelete(row) {
      this.$confirm("确认删除么?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        supplier
          .deleteById(row.id)
          .then(() => {
            this.getList();
            this.$notify({
              title: "提示",
              message: "操作成功",
              type: "success",
              duration: 2000
            });
          })
          .catch(() => {
            this.$notify({
              title: "提示",
              message: "操作失败",
              type: "error",
              duration: 2000
            });
          });
      });
    }
  }
};
</script>
